/*
 * GameModel.hpp
 *
 *  Created on: 27/dic/2012
 *      Author: stefano.pesenti
 */

#ifndef GAMEMODEL_HPP_
#define GAMEMODEL_HPP_

enum MAP_DETAIL_LEVEL {
	LOW, HIGH
};

typedef zen::video::Section Section;

class GameModel {
private:
	GameView* view;
	zen::video::Terrain* terrain;
	int cellPerTexture;

	int mapWidth;
	int mapHeight;

	int cellDimension;
	int initialXSection;
	int initialYSection;

	int minCellDistance;
	int maxCellDistance;
	MAP_DETAIL_LEVEL detail;

	bool loadVisibleMap();

public:
	GameModel() :
			view(0), terrain(0), cellPerTexture(zen::sys::Settings::getInt("cell_per_texture")), mapWidth(zen::sys::Settings::getInt("map_width")), mapHeight(
					zen::sys::Settings::getInt("map_height")), cellDimension(zen::sys::Settings::getInt("cell_dimension")), initialXSection(
					zen::sys::Settings::getInt("default_x_position")), initialYSection(zen::sys::Settings::getInt("current_y_position")), minCellDistance(
					zen::sys::Settings::getInt("min_cell_disatnce")), maxCellDistance(zen::sys::Settings::getInt("max_cell_distance")), detail(HIGH) {
	}

	~GameModel() {
	}

	void setDetailLevel(MAP_DETAIL_LEVEL);

	void updateCurrentPosition();

	int initialize(GameView* view);
	int update();

	int getMapWidth();
	int getMapheight();
	int getSectionDimension();
	int getVisibleRange();
	int getInitialXSection();
	int getInitialYSection();

	bool isSectionLoaded(int x, int y);
	bool loadSection(int x, int y, Section section);
	void drawTerrain(glm::vec3 cameraPosition);
};

void GameModel::setDetailLevel(MAP_DETAIL_LEVEL detail) {
	this->detail = detail;
}

int GameModel::initialize(GameView* view) {
	this->view = view;

	this->terrain = new zen::video::Terrain();
	zen::video::Section::setDimension(this->cellDimension);
	this->terrain->initialize(this->mapWidth, this->mapHeight, "data/textures/grass.bmp", this->cellPerTexture);

	return 0;
}

int GameModel::update() {
	return 0;
}

int GameModel::getMapWidth() {
	return this->mapWidth;
}

int GameModel::getMapheight() {
	return this->mapHeight;
}

int GameModel::getSectionDimension() {
	return this->cellDimension;
}

int GameModel::getVisibleRange() {
	return this->maxCellDistance;
}

int GameModel::getInitialXSection() {
	return this->initialXSection;
}

int GameModel::getInitialYSection() {
	return this->initialYSection;
}

bool GameModel::isSectionLoaded(int x, int y) {
	return this->terrain->isSectionLoaded(x, y);
}

bool GameModel::loadSection(int x, int y, Section section) {
	std::cout << "load section called" << " " << x << " " << y << std::endl;
	return this->terrain->loadSection(x, y, section);
}

void GameModel::drawTerrain(glm::vec3 cameraPosition) {

	this->terrain->draw((int) (cameraPosition.x / this->cellDimension) % this->mapWidth, (int) (cameraPosition.y / this->cellDimension) % this->mapHeight,
			this->maxCellDistance);
}

#endif /* GAMEMODEL_HPP_ */
